/* * Copyright 2014 Harald Wellmann. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. * * See the License for the specific language governing permissions and * limitations under the License. */ package org.ops4j.pax.web.itest; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.ops4j.pax.exam.CoreOptions.junitBundles; import static org.ops4j.pax.exam.CoreOptions.linkBundle; import static org.ops4j.pax.exam.CoreOptions.options; import static org.ops4j.pax.exam.CoreOptions.systemProperty; import static org.ops4j.pax.web.itest.util.TestConfiguration.logbackBundles; import static org.ops4j.pax.web.itest.util.TestConfiguration.paxUndertowBundles; import static org.ops4j.pax.web.itest.util.TestConfiguration.undertowBundles; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import javax.inject.Inject; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.servlet.ServletContext; import org.junit.Test; import org.junit.runner.RunWith; import org.ops4j.io.StreamUtils; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.PaxExam; import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; import org.ops4j.pax.exam.spi.reactors.PerClass; import org.ops4j.pax.web.itest.asset.DummyTrustManager; @RunWith(PaxExam.class) @ExamReactorStrategy(PerClass.class) public class SslTest { public final static TrustManager[] TRUST_ALL_CERTS = new X509TrustManager[] { new DummyTrustManager() }; @Inject private ServletContext servletContext; @Configuration public Option[] config() { return options( systemProperty("org.osgi.service.http.secure.enabled").value("true"), systemProperty("org.ops4j.pax.web.ssl.keystore").value("file:src/test/resources/server.keystore"), systemProperty("org.ops4j.pax.web.ssl.password").value("password"), linkBundle("pax-web-sample-static"), undertowBundles(), paxUndertowBundles(), logbackBundles(), junitBundles()); } @Test public void runStaticResourceServlet() throws Exception { URL url = new URL("https://localhost:8443/sample1/hello"); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); SSLContext ssl = SSLContext.getInstance("TLS"); ssl.init(null, TRUST_ALL_CERTS, null); con.setSSLSocketFactory(ssl.getSocketFactory()); con.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String s, SSLSession sslSession) { return true; } }); assertThat(servletContext.getContextPath(), is("/sample1")); InputStream is = con.getInputStream(); OutputStream os = new ByteArrayOutputStream(); StreamUtils.copyStream(is, os, true); assertThat(os.toString(), containsString("Hello from Pax Web!")); } }